//
//  AFNet.m
//  基于AF封装网络请求网络判断
//
//  Created by dlios on 15/6/6.
//  Copyright (c) 2015年 SJF1992. All rights reserved.
//

#import "AFNet.h"

@implementation AFNet

- (void)getUlr:(NSString *)url parameters:(NSDictionary *)param finishBolck:(myBlock)block
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //解密http 遇到这种加密方式就解密
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    NSString *str = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",  @"application/json", @"test/javascript", @"text/css", @"text/json", @"text/css",nil];
    [manager GET:str parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.netblock = block;
        block (responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"请求失败:%@",error);
//        UIAlertView * alert =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"HTTP请求错误,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [ alert show];
    }];
    
}

+ (void)connectNetGetDataWithURLString:(NSString *)urlStr parameters:(NSDictionary *)parameters finishblock:(myBlock)block
{
    AFNet *net = [[AFNet alloc] init];
    [net getUlr:urlStr parameters:parameters finishBolck:block];
}



- (void)postUlr:(NSString *)url parameters:(NSDictionary *)param finishBolck:(myBlock)block
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",  @"application/json", @"test/javascript", @"text/css", @"text/json", @"text/css",nil];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.netblock = block;
        block (responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView * alert =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"HTTP请求错误,请检查网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [ alert show];
    }];
}

+ (void)connectNetPostDataWithURLString:(NSString *)urlStr parameters:(NSDictionary *)parameters finishblock:(myBlock)block
{
    AFNet *net = [[AFNet alloc] init];
    [net postUlr:urlStr parameters:parameters finishBolck:block];
}


- (void)getReachbility
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
            {
                UIAlertView *alertNo = [[UIAlertView alloc] initWithTitle:@"网络状况" message:@"当前无网络,请重试" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [self performSelector:@selector(performDismiss:) withObject:alertNo afterDelay:2];
                [alertNo show];
                 break;
             }
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                UIAlertView *alertWifi = [[UIAlertView alloc] initWithTitle:@"网络状况" message:@"当前网络为免费Wifi" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [self performSelector:@selector(performDismiss:) withObject:alertWifi afterDelay:2];
                [alertWifi show];
                break;
             }
        
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                UIAlertView *alertWan = [[UIAlertView alloc] initWithTitle:@"网络状况" message:@"当前网络为无线网" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [self performSelector:@selector(performDismiss:) withObject:alertWan afterDelay:2];
                [alertWan show];
                break;
            }
                default:
                break;
        }
    
    }];
}


- (void)performDismiss:(UIAlertView *)alert
{
    
    [alert dismissWithClickedButtonIndex:0 animated:NO];
    
}


@end
